<template>
  <div class="player">
    <div id="player"></div>
  </div>
</template>

<script>
  export default {
    name: "Player",
    data() {
      return {
        // token: sessionStorage.token || localStorage.token,
        // user_id: sessionStorage.id || localStorage.id,
        // user_name: sessionStorage.username || localStorage.username,
      }
    },
    methods: {},
    created() {
      if (!this.$store.state.user.token) {
        let _this = this;
        this.$alert('对不起，您尚未登录！请登录！', '警告', {
          callback() {
            _this.$router.push('/login');
          }
        });
      }
    },
    mounted() {
      let _this = this;
      let video_id = this.$route.query.vid;
      var player = polyvObject('#player').videoPlayer({
        wrap: '#player',
        width: document.documentElement.clientWidth, // 宽度
        height: document.documentElement.clientHeight, // 高度
        forceH5: true,
        vid: video_id,
        code: this.$store.state.user.username, // 一般是用户昵称
        // 视频加密播放的配置
        playsafe: function (vid, next) { // 向后端发送请求获取加密的token
          if (!_this.$store.state.user.token) {return}
          _this.$axios.get(_this.$settings.Host + `/courses/polyv/token/`, {
            headers: {
              'Authorization': 'jwt ' + _this.$store.state.user.token
            },
            params: {
              vid: video_id,
            }
          }).then(function (response) {
            next(response.data.token);
          }).catch(error => {
            console.log(error.response.data.detail)
          })

        }
      });
    },
    computed: {}
  }
</script>

<style scoped>
  #player {
     margin-top: -80px;
  }
</style>
